/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.ui.option.flush;

import com.yy.bugzillahelp.MainFrame;
import com.yy.bugzillahelp.timer.FixedTimerEvent;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author hualun-alan
 */
public class FlushSettingPanel extends javax.swing.JPanel {

    private static final String[] TIME_INTERVAL = new String[]{"1", "10", "15", "30", "60", "120", "240"};//, "Customize"
    private static final String TIMER_FLUSH = "TIMER_FLUSH";
    private String preSelectedItem = "";
    private String curSelectedItem = "";

    /**
     * Creates new form FlushSettingPanel
     */
    public FlushSettingPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jp2 = new javax.swing.JPanel();
        jcbAutoFlush = new javax.swing.JCheckBox();
        jp3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcmbInterval = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jp1.setPreferredSize(new java.awt.Dimension(400, 30));
        jp1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel2.setText("You can set flush property in this page.");
        jp1.add(jLabel2);

        add(jp1);

        jp2.setPreferredSize(new java.awt.Dimension(400, 30));
        jp2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jcbAutoFlush.setSelected(true);
        jcbAutoFlush.setText("Auto-flush on");
        jcbAutoFlush.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbAutoFlushActionPerformed(evt);
            }
        });
        jp2.add(jcbAutoFlush);

        add(jp2);

        jp3.setPreferredSize(new java.awt.Dimension(400, 30));
        jp3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("Auto-flush time interval:");
        jp3.add(jLabel1);

        jcmbInterval.setModel(new DefaultComboBoxModel(TIME_INTERVAL));
        jcmbInterval.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbIntervalItemStateChanged(evt);
            }
        });
        jp3.add(jcmbInterval);

        jLabel3.setText("minutes.");
        jp3.add(jLabel3);

        add(jp3);
    }// </editor-fold>//GEN-END:initComponents

    private void jcbAutoFlushActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbAutoFlushActionPerformed
        boolean selected = !jcbAutoFlush.isSelected();
        int componentCount = jp3.getComponentCount();
        for (int i = 0; i < componentCount; i++) {
            Component component = jp3.getComponent(i);
            component.setEnabled(selected);
        }

        if (selected) {
            FlushTimerEvent flushTimerEvent = new FlushTimerEvent();
            MainFrame.getFrame().getTimerCenter().addTimerEvent(TIMER_FLUSH, new FlushTimerEvent());
        } else {
            MainFrame.getFrame().getTimerCenter().removeTimerEvent(TIMER_FLUSH);
        }
    }//GEN-LAST:event_jcbAutoFlushActionPerformed

    private void jcmbIntervalItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbIntervalItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            curSelectedItem = jcmbInterval.getSelectedItem().toString();

            MainFrame.getFrame().getTimerCenter().removeTimerEvent(TIMER_FLUSH);
            FlushTimerEvent flushTimerEvent = new FlushTimerEvent();
            MainFrame.getFrame().getTimerCenter().addTimerEvent(TIMER_FLUSH, new FlushTimerEvent());
        }
    }//GEN-LAST:event_jcmbIntervalItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JCheckBox jcbAutoFlush;
    private javax.swing.JComboBox jcmbInterval;
    private javax.swing.JPanel jp1;
    private javax.swing.JPanel jp2;
    private javax.swing.JPanel jp3;
    // End of variables declaration//GEN-END:variables

    private class FlushTimerEvent extends FixedTimerEvent {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("FlushTimerEvent");
        }
    }
}
